package qq.service;

import org.apache.ibatis.session.SqlSession;
import qq.bean.User;
import qq.mapper.UserMapper;
import qq.tool.MybatisTool;

import java.io.IOException;

public class RPGService {
    public static synchronized String dajie(long id1,long id2) throws IOException {
        User user1 = UserSevice.getUser(id1);
        User user2 = UserSevice.getUser(id2);
        if (user1.getPoints()<5 || user2.getPoints()<5){
           return "你的积分或者对方积分不足";
        }
        int hurt1 = (int)(user1.getAttack()-user2.getDefense()* Math.random())+5;
        int hurt2 = (int)(user2.getAttack()-user1.getDefense()* Math.random())+5;
        if(hurt1<0)
            hurt1=3;
        if (hurt2<0){
            hurt2=3;
        }
        double count1 = user1.getLife()*1.0/hurt2;
        double count2 = user2.getLife()*1.0/hurt1;
        if (count1>count2){
            UserSevice.declinePoint(id2,5);
            UserSevice.addPoint(id1,5);
            return "打劫成功,抢到5积分";
        }else {
            UserSevice.declinePoint(id1,5);
            UserSevice.addPoint(id2,5);
            return "打劫失败，被抢走5积分";
        }

    }

    public static synchronized String choujiang(long id) throws IOException {
        String result="很遗憾哦，你什么都没抽中";
        SqlSession sqlSession = MybatisTool.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        if (UserSevice.getUser(id).getPoints()<5){
            return "对不起你的积分不足";
        }
        UserSevice.declinePoint(id,5);
        int random = (int)(Math.random()*11);
        if (random == 1){
            userMapper.addAttack(id,2);
            result = "消耗5点积分,增加2点攻击";
        }else if (random ==2 ){
            userMapper.addDefense(id,2);
            result = "消耗5点积分,增加2点防御";
        }else if(random == 3){
            userMapper.addLife(id,10);
            result = "消耗5点积分,增加10点生命值";
        }
        sqlSession.commit();
        return result;
    }
}
